package jdbc;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtility {

    public static Connection getConnection()  {
        Connection conn = null;
        try {
            InputStream is = JDBCUtility.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties property = new Properties();
            property.load(is);

            String user = property.getProperty("user");
            String password = property.getProperty("password");
            String url = property.getProperty("url");
            String driverClass = property.getProperty("driverClass");

            Class.forName(driverClass);

            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void getClose(PreparedStatement ps , ResultSet rs , Connection conn){
        try {
            if (ps != null){
                ps.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (rs != null){
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (conn != null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void getClose(PreparedStatement ps , Connection conn){
        try {
            if (ps != null){
                ps.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            if (conn != null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
